package com.example.controller;


import com.example.entity.User;
import com.example.entity.dto.LoginDto;
import com.example.service.IUserService;
import com.example.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author sword
 * @since 2022-02-19
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户")
@RequiredArgsConstructor
public class UserController {
    private final IUserService userService;

    @ApiOperation("列表")
    @GetMapping("/list")
    public Result<List<User>> list() {
        return Result.success(userService.list());
    }

    @PostMapping("/register")
    public Result registerUser(@RequestBody User registerUser) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        registerUser.setPassword(bCryptPasswordEncoder.encode(registerUser.getPassword()));
        boolean i = userService.save(registerUser);
        if (i) {
            return Result.success();
        }
        return Result.error("插入失败");
    }

    @PostMapping("/login")
    public Map login(@RequestBody LoginDto adminLoginParam, HttpServletRequest request) {
        return userService.login(adminLoginParam.getUsername(), adminLoginParam.getPassword(), request);
    }

    //测试只有登录拿到token才能拿到数据
    @GetMapping("/get")
    public String aaa() {
        return "token验证通过了";
    }

    @PostMapping("/logout")
    public Result logoutSuccess() {
        return Result.success(200, "退出成功！！！");
    }
}
